home *** CD-ROM | disk | FTP | other *** search
/ Web Designer 98 (Professional) / WebDesigner 1.0.iso / cgi2 / download.cgi-s=textcounter&c=txt&f=counter.pl < prev    next >
Encoding:
Perl Script  |  1996-06-04  |  7.6 KB  |  235 lines

  1. #!/usr/local/bin/perl
  2. ##############################################################################
  3. # TextCounter                   Version 1.2                                  #
  4. # Copyright 1996 Matt Wright    mattw@worldwidemart.com                      #
  5. # Created 3/14/96               Last Modified 5/10/96                        #
  6. # Scripts Archive at:           http://www.worldwidemart.com/scripts/        #
  7. ##############################################################################
  8. # COPYRIGHT NOTICE                                                           #
  9. # Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
  10. #                                                                            #
  11. # TextCounter may be used and modified free of charge by anyone so long as   #
  12. # this copyright notice and the comments above remain intact.  By using this #
  13. # code you agree to indemnify Matthew M. Wright from any liability that      #  
  14. # might arise from it's use.                                                 #  
  15. #                                                                            #
  16. # Selling the code for this program without prior written consent is         #
  17. # expressly forbidden.  In other words, please ask first before you try and  #
  18. # make money off of my program.                                              #
  19. #                                                                            #
  20. # Obtain permission before redistributing this software over the Internet or #
  21. # in any other medium.  In all cases copyright and header must remain intact.#
  22. ##############################################################################
  23. # Define Variables
  24.  
  25. # Data Dir is the directory on your server that you wish to store the 
  26. # count files in.  Each page that has the counter on it will have it's own 
  27. # file.  
  28.  
  29. $data_dir = "/path/to/textcounter/data/";
  30.  
  31. # Valid-URI allows you to set up the counter to only work under specific 
  32. # directories on your server.  Include any of these directories as they 
  33. # appear in a URI, into this array.  More information on URI's available in 
  34. # README.
  35.  
  36. @valid_uri = ('/');
  37.  
  38. # Invalid-URI allows the owner of this script to set up the counter so 
  39. # that certain portions of the web server that may be included in Valid-URI 
  40. # cannot use the program.  Leave this commented out if you wish not to 
  41. # block out certain parts.
  42.  
  43. # @invalid_uri = ('/');
  44.  
  45. ##############################################################################
  46. # Set Options
  47.  
  48. # Show Link allows you to add a link around the counter to point to 
  49. # either instructions explaining to users how to set this up on the system 
  50. # (useful if a system administrator wants to allow anyone to set things up 
  51. # themselves).  Setting it to 0 will make no link, otherwise put the URL
  52. # you want linked to the count here.
  53.  
  54. $show_link = "http://www.worldwidemart.com/scripts/";
  55.  
  56. # When Auto-Create is enabled, users will be able to auto-create the 
  57. # count on their home pages by simply imbedding the Server Side Includes 
  58. # call.  Setting auto_create to 1 enables it, 0 will disable it. Only 
  59. # users in @valid_uri will be allowed to auto create.
  60.  
  61. $auto_create = "1";
  62.  
  63. # Show Date will show the date of when the count began if you set this 
  64. # option to 1.  It will appear in yor document as [Count] hits since [Date].
  65. # Set this to 0 and it will simply return the [Count].
  66.  
  67. $show_date = "1";
  68.  
  69. # Lock Seconds will define the number of seconds the script should sit 
  70. # and wait for the lock file to be gone before it will overwrite it if it 
  71. # is still there.  Every now and then a user will interrupt a page, causing 
  72. # the script to halt and leave a lock file in place before the lock file 
  73. # could be removed.  This defines how long it waits.
  74.  
  75. $lock_sec = "3";
  76.  
  77. # Padding Size define how many numbers will be shown as your count.  For 
  78. # instance, if you want your count to say 00065 and have the zeros padded 
  79. # up to five digits, then set $pad_size = "5";  If the number goes higher 
  80. # than the pad_size, don't worry, there just won't be any zero's tacked 
  81. # onto the front.
  82.  
  83. $pad_size = "5";
  84.  
  85. ##############################################################################
  86.  
  87. # Print Content Type Header For Browser
  88. print "Content-type: text/html\n\n";
  89.  
  90. # Get the page location from the DOCUMENT_URI environment variable.
  91. $count_page = "$ENV{'DOCUMENT_URI'}";
  92.  
  93. # Chop off any trailing /'s
  94. if ($count_page =~ /\/$/) {
  95.    chop($count_page);
  96. }
  97.  
  98. $count_page =~ s/\//_/g;
  99. $lock_file = "$count_page\.lock";
  100.  
  101. # Check Valid-URI to make sure user can use this program.
  102. &check_uri;
  103.  
  104. # Check to see if file is locked by program already in use.
  105. &check_lock($lock_sec);
  106.  
  107. # If the file exists, get the date and count out of it.  Otherwise, if 
  108. # auto_create is allowed, create a new account.  If neither of these are 
  109. # true, return an error.
  110.  
  111. if (-e "$data_dir$count_page") {
  112.    open(COUNT,"$data_dir$count_page");
  113.    $line = <COUNT>;
  114.    chop($line) if $line =~ /\n$/;
  115.    close(COUNT);
  116.    ($date,$count) = split(/\|\|/,$line);
  117. }
  118. elsif ($auto_create == 1) {
  119.    &create; 
  120. }
  121. else {
  122.    &error('page_not_found');
  123. }
  124.  
  125. # Increment Count.
  126. $count++;
  127. $print_count = $count;
  128.  
  129. # Get Count Length for use in padding.
  130. $count_length = length($count);
  131.  
  132. # Pad the number if it is smaller than $pad_size.
  133. for ($i = $pad_size;$i > $count_length;$i--) {
  134.    $print_count = "0$print_count";
  135. }
  136.  
  137. # Print the Count, Link and Date depending on what user has specified 
  138. # they wish to print.
  139.  
  140. if ($show_date == 1) {
  141.    if ($show_link =~ /http:\/\//) {
  142.       print "<a href=\"$show_link\">$print_count</a> hits since $date";
  143.    }
  144.    else {
  145.       print "$print_count hits since $date";
  146.    }
  147. }
  148. else {
  149.    if ($show_link =~ /http:\/\//) {
  150.       print "<a href=\"$show_link\">$print_count</a>";
  151.    }
  152.    else {
  153.       print "$print_count";
  154.    }
  155. }
  156.  
  157. # Open the count file and write the new count that has been incremented.
  158. open(COUNT,">$data_dir$count_page") || &error('could_not_increment');
  159. print COUNT "$date\|\|$count";
  160. close(COUNT);
  161.  
  162. # Remove Lock File for next time script is run on that HTML page.
  163. &clean_up;
  164.  
  165. sub check_uri {
  166.    $uri_check = "0";
  167.  
  168.    foreach $uri (@valid_uri) {
  169.       if ($ENV{'DOCUMENT_URI'} =~ /$uri/) {
  170.          $uri_check = "1";
  171.          last;
  172.       }
  173.    }
  174.  
  175.    foreach $uri (@invalid_uri) {
  176.       if ($ENV{'DOCUMENT_URI'} =~ /$uri/) {
  177.          $uri_check = "0";
  178.      last;
  179.       }
  180.    }
  181.  
  182.    if ($uri_check == 0) {
  183.       &error('bad_uri');
  184.    }
  185. }
  186.  
  187. sub create {
  188.    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  189.    @months = ("January","February","March","April","May","June","July",
  190.           "August","September","October","November","December");
  191.    $date = "@months[$mon] $mday, 19$year";
  192.    $count = "0";
  193.    open(COUNT,">$data_dir$count_page") || &error('count_not_created');
  194.    print COUNT "$date\|\|$count";
  195.    close(COUNT);
  196. }
  197.  
  198. sub error {
  199.    $error = shift(@_);
  200.  
  201.    if ($error eq 'page_not_found') {
  202.       print "[TextCounter Fatal Error: This Page Not Found\; Auto-Create Option Disabled]";
  203.    }
  204.    elsif ($error eq 'bad_uri') {
  205.       print "[TextCounter Fatal Error: This Page Not In Valid URI]";
  206.    }
  207.    elsif ($error eq 'count_not_created') {
  208.       print "[TextCounter Fatal Error: Could Not Write to File $datadir$count_page]";
  209.    }
  210.    elsif ($error eq 'could_not_increment') {
  211.       print "[TextCounter Fatal Error: Could Not Increment Counter]";
  212.    }
  213.    exit;
  214. }
  215.  
  216. sub check_lock {
  217.    $time = $_[0];
  218.  
  219.    for ($i = 1;$i <= $time; $i++) {
  220.       if (-e "$data_dir$lock_file") {
  221.          sleep 1;
  222.       }
  223.       else {
  224.          open(LOCK,">$data_dir$lock_file");
  225.          print LOCK "0";
  226.          close(LOCK);
  227.          last;
  228.       }
  229.    }
  230. }
  231.  
  232. sub clean_up {
  233.    unlink("$data_dir$lock_file");
  234. }
  235.